/* This module is part of the Project Krishi Suvidha and deals with
 * XML parsing of the Menu Configuration file and based on the information
 * obtained from the parsed configuration file we instantiate the menu.
 * This file has been written by : Ambuj Varshney < ambuj_varshney@daiict.ac.in >
 * (C), 2010 Ambuj Varshney and released under the licensing terms of GPL v 3.0
 */

/* Here goes the format of the XML file that is to be parsed 

*/

import org.kxml2.io.*;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import java.io.*;
import java.util.Vector;

public class menuxmlparse {
	
	// This holds object of our XML Parser
	private KXmlParser xmlp = null;
	private int no_of_menu_item;
//	private Vector tmenu_name = new Vector();
//	private Vector taudio_file = new Vector();
//	private Vector ticon_name = new Vector();
	
	public menuxmlparse( Vector tmenu_name , Vector taudio_file , Vector ticon_name, int no_elem, String Xml_file)
	{
		 
		System.out.println("In Menu-Xml_parse");
		
		System.out.println("XmlPullParserEnd = " + XmlPullParser.END_TAG);
		System.out.println("XmlPullParserStart = " + XmlPullParser.START_TAG);
		System.out.println("XmlPullParsr TEXT = " + XmlPullParser.TEXT);
		
		InputStream in =  getClass().getResourceAsStream(Xml_file);
		InputStreamReader is =  new InputStreamReader(in);
		
		System.out.println("Reached Point 1");
		
		xmlp = new KXmlParser();
		try {
			 
			
				xmlp.setInput(is); // Associate the InputStreamReader with this XML Parser
			    xmlp.nextTag(); // We go to next Tag
			    xmlp.require(XmlPullParser.START_TAG, null , "krishimenu"); // This says that we require krishimenu tag to begin
			    
			    xmlp.nextTag();
			     
			    if(xmlp.getName().compareTo("no_of_menu_item") == 0)
			    {
			    	String s1 = xmlp.nextText();
			    	System.out.println("String parsed is = " + s1);
			    	no_elem = Integer.parseInt(s1.substring(2, 3));
			       	//no_of_menu_item = Integer.parseInt(s1.substring(i));
			    		    	 
			    }
			    
			   if(xmlp.next() == XmlPullParser.END_TAG)
				   System.out.println("End tag encountered");
			   
			    int i=0; // this is a random variable denoting number of menu items read so far
			   // xmlp.nextTag();
			    
			    //System.out.println("Start -- " + xmlp.nextText());
			    
			    while(xmlp.next() != XmlPullParser.END_DOCUMENT)
			    {
			    	System.out.println("Loop Entered");
			    	System.out.println("Event type = " + xmlp.getEventType());
			    	
			    		
			    	if(xmlp.getEventType() == XmlPullParser.START_TAG)
			    	{
			     		String gt_name = xmlp.getName();
			    		System.out.println("get name right now = " + gt_name);
			       		if( gt_name.compareTo("menu_item") == 0)
			    		{
			    			if(xmlp.getAttributeCount() > 0 ) // There are more than one attribute associated with the Menu item
			    			{
			    				String s1 = xmlp.getAttributeName(0);
			    				String s2 = xmlp.getAttributeName(1);
			    				
			    				String s3 = xmlp.getAttributeValue(0);
			    				String s4 = xmlp.getAttributeValue(1);
			    				 
			    				taudio_file.addElement(s3);
			    				ticon_name.addElement(s4);
			    				
			    				System.out.println(" --- " + s3  + "---" + s4);
			    				
			    			
			    			}
			    			
			    			String s5 =  xmlp.nextText();
			    			System.out.println("Boo -- " + s5);
			    			tmenu_name.addElement(s5);
			    			
			    			
			    			System.out.println("Read So far: " + tmenu_name.elementAt(i) + " " + taudio_file.elementAt(i) + " " + ticon_name.elementAt(i));
			    		 	
			    			i++;
			    		}
			    	    		
			    	}
			    	if(xmlp.getEventType() == XmlPullParser.END_TAG)
		    		{
		    			System.out.println("End Tag Encountered");
		    			//xmlp.nextTag();
//		    			xmlp.next();
		    			xmlp.next();
		    			
			     		System.out.println("Next tag is = " + xmlp.getEventType());
		    		}
		    			    	
		    		
			    }
			     
			    
		}catch(Exception e)
		{
			e.printStackTrace();
		}
		  
	//	menu_name = tmenu_name;
    //	audio_file = taudio_file;
    //	icon_name = ticon_name;
    //	System.out.println("Element at from menuxml = " + tmenu_name.elementAt(0));
    	
}

	
	
	 
}
